package com.talent.proxy.mysql.common.packets.s2c;

import java.nio.ByteBuffer;

import com.talent.aio.common.ChannelContext;
import com.talent.proxy.mysql.common.MysqlExt;
import com.talent.proxy.mysql.common.MysqlPacket;
import com.talent.proxy.mysql.common.MysqlReturn;
import com.talent.proxy.mysql.common.utils.BufferUtil;

/**
 * 
 * <pre>
 * <table class="informaltable" border="0">
<colgroup>
<col class="type">
<col class="name">
<col class="desc">
</colgroup>
<thead><tr>
<th scope="col">Type</th>
<th scope="col">Name</th>
<th scope="col">Description</th>
</tr></thead>
<tbody>
<tr>
<td scope="row"><a class="link" href="describing-packets.html#type-1"><span class="type">int&lt;1&gt;</span></a></td>
<td><em class="structfield"><code><a name="cs-packet-eof-header"></a>header</code></em></td>
<td>
<code class="literal">[fe]</code> EOF header</td>
</tr>
<tr><td scope="row" colspan="3"><code class="code">if capabilities &amp;
                  <a class="link" href="capability-flags.html#flag-CLIENT_PROTOCOL_41">CLIENT_PROTOCOL_41</a>
                  {</code></td></tr>
<tr>
<td scope="row"><a class="link" href="describing-packets.html#type-2"><span class="type">&nbsp;&nbsp;int&lt;2&gt;</span></a></td>
<td><em class="structfield"><code><a name="cs-packet-eof-warningcount"></a>warnings</code></em></td>
<td>number of warnings</td>
</tr>
<tr>
<td scope="row"><a class="link" href="describing-packets.html#type-2"><span class="type">&nbsp;&nbsp;int&lt;2&gt;</span></a></td>
<td><em class="structfield"><code><a name="cs-packet-eof-statusflags"></a>status_flags</code></em></td>
<td><a class="link" href="status-flags.html" title="14.1.3.4&nbsp;Status Flags">Status Flags</a></td>
</tr>
<tr><td scope="row" colspan="3"><code class="code">}</code></td></tr>
</tbody>
</table>
 * 
 * </pre>
 * @see http://dev.mysql.com/doc/internals/en/packet-EOF_Packet.html
 */
public class EOFPacket extends MysqlPacket
{

	public static final byte MARK = (byte) 0xfe;

	public byte header = (byte) 0xfe;
	public int warningCount;
	public int status = 2;

	@Override
	public void decodeBody(ByteBuffer buffer, ChannelContext<MysqlExt, MysqlPacket, MysqlReturn> channelContext)
	{

		header = BufferUtil.read(buffer);
		warningCount = BufferUtil.readUB2(buffer);
		status = BufferUtil.readUB2(buffer);
	}

	@Override
	public ByteBuffer encode(ChannelContext<MysqlExt, MysqlPacket, MysqlReturn> channelContext)
	{
		int bodySize = bodySize();
		ByteBuffer buffer = ByteBuffer.allocate(HEADER_SIZE + bodySize);
		BufferUtil.writeUB3(buffer, bodySize);
		buffer.put(seqId);
		buffer.put(header);
		BufferUtil.writeUB2(buffer, warningCount);
		BufferUtil.writeUB2(buffer, status);
		return buffer;
	}

	@Override
	public int bodySize()
	{
		return 5;
	}

	@Override
	protected String packetDesc()
	{
		return "MySQL EOF Packet";
	}

	/** 
	 * @see com.talent.proxy.mysql.common.MysqlPacket#onReceived(com.talent.aio.common.ChannelContext)
	 * 
	 * @param channelContext
	 * @return
	 * @重写人: tanyaowu
	 * @重写时间: 2017年1月23日 下午4:59:45
	 * 
	 */
	@Override
	public MysqlReturn onReceived(ChannelContext<MysqlExt, MysqlPacket, MysqlReturn> channelContext)
	{
		// TODO Auto-generated method stub
		return null;
	}

}
